package com.cmc.cloud.cmclink.doc.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.entity.AgentContactDO;
import com.cmc.cloud.cmclink.doc.service.AgentContactService;
import com.cmc.cloud.cmclink.doc.vo.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.excel.core.util.ExcelUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;


/**
 * {table.classComment}前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 航线进出口联系人信息")
@RestController
@RequestMapping(UrlConstants.APPLICATION_URL + "/agentContact")
@Validated
public class AgentContactController {

    @Resource
    private AgentContactService agentContactService;

    @Operation(summary = "创建航线进出口联系人信息")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission({'doc:setupLaneContactPerson:add','doc:FCN/MCNNotify:add','doc:freightCalculationNotice:add','doc:manifestLockNotify:add','doc:billRemark:add'})")
    public CommonResult<Void> createAgentContact(@Valid @RequestBody AgentContactCreateReqVO createReqVO) {
        agentContactService.createAgentContact(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新航线进出口联系人信息")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission({'doc:setupLaneContactPerson:edit','doc:FCN/MCNNotify:edit','doc:freightCalculationNotice:edit','doc:manifestLockNotify:edit','doc:billRemark:edit','doc:billRemark:save'})")
    public CommonResult<Void> updateAgentContact(@Valid @RequestBody AgentContactUpdateReqVO updateReqVO) {
        agentContactService.updateAgentContact(updateReqVO);
        return CommonResult.success();
    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "删除航线进出口联系人信息")
    @PreAuthorize("@ss.hasPermission({'doc:setupLaneContactPerson:delete','doc:FCN/MCNNotify:delete','doc:billRemark:delete'})")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> deleteAgentContact(@PathVariable("id") Long id) {
        agentContactService.deleteAgentContact(id);
        return CommonResult.success();
    }

    @GetMapping("/getById/{id}")
    @Operation(summary = "获得航线进出口联系人信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AgentContactRespVO> getAgentContact(@PathVariable("id") Long id) {
        AgentContactDO agentContact = agentContactService.getAgentContact(id);
        return CommonResult.success(BeanUtil.copyProperties(agentContact, AgentContactRespVO.class));
    }

    @GetMapping("/listAll")
    @Operation(summary = "获得航线进出口联系人信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<AgentContactRespVO>> getAgentContactList(@RequestParam("ids") Collection<Long> ids) {
        List<AgentContactDO> list = agentContactService.getAgentContactList(ids);
        return CommonResult.success(BeanUtil.copyToList(list, AgentContactRespVO.class));
    }

    @Operation(summary = "获得航线进出口联系人信息分页")
    @PostMapping("/getPage")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission({'doc:FCN/MCNNotify:search','doc:freightCalculationNotice:search','doc:manifestLockNotify:search'})")
    public CommonResult<PageResult<AgentContactRespVO>> getAgentContactPage(@Valid @RequestBody AgentContactPageReqVO pageVO) {
        PageResult<AgentContactDO> pageResult = agentContactService.getAgentContactPage(pageVO);
        List<AgentContactRespVO> results = BeanUtil.copyToList(pageResult.getList(), AgentContactRespVO.class);
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "获得航线进出口联系人专用信息分页")
    @PostMapping("/personPage")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:setupLaneContactPerson:search')")
    public CommonResult<PageResult<AgentContactRespVO>> personPage(@Valid @RequestBody AgentContactPageReqVO pageVO) {
        PageResult<AgentContactDO> pageResult = agentContactService.personPage(pageVO);
        List<AgentContactRespVO> results = BeanUtil.copyToList(pageResult.getList(), AgentContactRespVO.class);
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "获得进出口客户费用备注分页")
    @PostMapping("/remarkPage")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:billRemark:search')")
    public CommonResult<PageResult<AgentContactRespVO>> remarkPage(@Valid @RequestBody AgentContactPageReqVO pageVO) {
        PageResult<AgentContactDO> pageResult = agentContactService.remarkPage(pageVO);
        List<AgentContactRespVO> results = BeanUtil.copyToList(pageResult.getList(), AgentContactRespVO.class);
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }


    @PostMapping("/excel-export")
    @Operation(summary = "excel导出")
    @PreAuthorize("@ss.hasPermission('doc:setupLaneContactPerson:export')")
    public void exportExcel(HttpServletResponse response, @RequestBody AgentContactReqVO agentContactReqVO) throws IOException {
        List<AgentContactExcelRespVO> exportList = agentContactService.exportExcel(agentContactReqVO);
        // FIXME 一定要向response写数据之前设置响应头，否则会出现文件下载失败
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("agentContact.xlsx", StandardCharsets.UTF_8.name()));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ExcelUtils.write(response, "agentContact.xls", "agentContact", AgentContactExcelRespVO.class, exportList);
    }
}
